layui.use(['table','form','layer','laydate','laytpl','element'], function() {
	var table = layui.table;
	var table2=layui.table;
	form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    laytpl = layui.laytpl,
    element = layui.element;
	
    laydate.render({
	   elem: '#inpeditbirthday'
	});

    /*动态加载班级下拉框*/
	form.on('select(selgradename)', function(data) {
		var schoolid = $("#selschool").val();
		var gradname=data.value;
		if(schoolid==00 || schoolid=='null' || schoolid==null){
			layer.msg("请选择学校");
			return;
		}
		if(gradname==00 || gradname=='null' || gradname==null){
			gradname="";
		}
		$("#classid").html("");
		$.ajax({
			type : 'get',
			url : '../sysclassmanagement/getclassbyschoolandgrade?schoolid=' + schoolid+'&gradename='+gradname,
			datatype : 'json',
			success : function(classdata) {
				if (classdata.code == 0) {
					var str = "<option value='00'>请选择班级</option>";
					for(var i = 0; i < classdata.count; i++){
						str += '<option value=' + classdata.data[i].classid + '>'+classdata.data[i].className +'</option>';
					}
					$("#classid").append(str);
					form.render();
				}
				else{
					layer.msg('暂无对应的班级', {icon: 2});
					$("#classid").html("");
					form.render("select");
				}
			}
		});
	});
    
	//点击查询筛选家长信息
	$("#query").click(function() {
		var schoolid=$("#selschool").val();
		var gradename=$("#selgradename").val();
		var classid=$("#classid").val();
		var userName = $("#seleUserName").val().trim();
		if(schoolid==00 || schoolid==null || schoolid==undefined || schoolid=='null'){
			schoolid="";
		}
		if(gradename==00 || gradename==null || gradename==undefined || gradename=='null'){
			gradename=""
		}
		if(classid==00 || classid==null || classid==undefined  || classid=='null'){
			classid="";
		}
		table.render({
			id:'parinfoid',
			elem: '#parent',
			url: '../sysparentmanagement/getparentbywhere?schoolid='+schoolid+'&gradename='+gradename+'&classid='+classid+'&userName='+userName,
			title: '家长数据表',
			height:"full-140",
			skin: 'line',
			even: true,
			cols: [
				[{
					type:'numbers',
					align: "center",
					title:'序号'
				},{
					field: 'parUserName',
					title: '账号',
					align: "center"
				}, {
					field: 'parRealName',
					title: '真实姓名',
					align: "center"
				}, {
					field: 'parSex',
					title: '性别',
					align: "center"
				},  {
					field: 'className',
					title: '班级',
					align: "center"					
				},{
					field: 'gradename',
					title: '年级',
					align: "center"					
				},{
					field: 'schoolName',
					title: '学校',
					align: "center"					
				},{
					field: 'phone',
					title: '电话',
					align: "center"					
				},
				{
					title: '操作',
					toolbar: '#barDemo',
					width:"18%",
					align: "center"
				}]
			],
			page: {
				layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
				groups: 5,
				limit: 10,
				limits: [10, 20, 30, 40, 50],
				theme: '#1E9FFF',						
			}
		});
	});
	
    //监听行工具事件
    table.on('tool(parent)', function(obj){
	    var data = obj.data;
	    /*查看家长信息详情*/
	    if(obj.event === 'btnselparent'){
	    	var parentid=data.parentid;
			var schoolid=data.schoolid;
			$.ajax({
				type : 'get',
				url : '../sysparentmanagement/getaparentbyid?parentid='+parentid+'&schoolid='+schoolid,
				datatype : 'json',
				success : function(parentdata) {
					if (parentdata.code == 0) {
						$("#userName").text(parentdata.data[0].userName);
						$("#realName").text(parentdata.data[0].realName);
						$("#laysex").text(parentdata.data[0].sex);
						$("#email").text(parentdata.data[0].email);
						$("#phone").text(parentdata.data[0].phone);
						$("#birthday").text(parentdata.data[0].birthday);
						$("#address").text(parentdata.data[0].address);
						$("#tell").text(parentdata.data[0].tell);
						$("#workTel").text(parentdata.data[0].workTel);
						$("#workUnit").text(parentdata.data[0].workUnit);
						$("#education").text(parentdata.data[0].education);
					}
					else{
						layer.msg("家长信息获取失败");
					}
				}
			});
			
			var index=layer.open({
		        type: 1, 
		        title: '家长信息详情',
		        shade: 0.8,
		        maxmin:true,
		        content: $('#parentlayercon'),
		        cancel: function(){ 
				   $(".parentlayer-con").css("display","none");
				}
		    });
			layer.full(index);
			
			table2.render({
				elem: '#parentStus',
				url: '../sysparentmanagement/getparentstudent?parentid='+parentid,
				title: '家长学生数据表',
				skin: 'line',
				even: true,
				cols: [
					[{
						type:'numbers',
						align: "center",
						width:"10%",
						title:'序号'
					}, {
						field: 'stuid',
						align: "center",
						width:"20%",
						title: '学号'
					}, {
						field: 'stuRealName',
						align: "center",
						width:"20%",
						title: '真实姓名'
					}, {
						field: 'stuSex',
						align: "center",
						width:"10%",
						title: '性别'
					}, 
					{
						field: 'gradename',
						align: "center",
						width:"10%",
						title: '班级'
					},{
						field: 'className',
						align: "center",
						width:"20%",
						title: '年级'
					},{
						field: 'parRealName',
						align: "center",
						width:"10%",
						title: '家长姓名'
					}]
				],
			});
	    }
	    /*修改家长信息*/
	    else if(obj.event === 'btneditparent'){
	    	var parentid=data.parentid;
			var schoolid=data.schoolid;
	 		$.ajax({
	 			type : 'get',
	 			url : '../sysparentmanagement/getaparentbyid?parentid='+parentid+'&schoolid='+schoolid,
	 			datatype : 'json',
	 			success : function(parentdata) {
	 				if (parentdata.code == 0) {
	 					$("#inpedituserName").val(parentdata.data[0].userName);
	 					$("#inpeditrealName").val(parentdata.data[0].realName);
	 					$("#inpeditbirthday").val(parentdata.data[0].birthday);
	 					$("#inpeditphone").val(parentdata.data[0].phone);
	 					$("#inpeditaddress").val(parentdata.data[0].address);
	 					$(".inpeditsex").each(function(){
	 						if($.trim($(".inpeditsex")[0].value) == $.trim(parentdata.data[0].sex)){
	 							$(".inpeditsex")[0].checked = true;
	 							form.render("radio");
	 						}
	 						if($.trim($(".inpeditsex")[1].value) == $.trim(parentdata.data[0].sex)){
	 							$(".inpeditsex")[1].checked = true;
	 							form.render("radio");
	 						}
	 					});
	 				}
	 				else{
	 					layer.msg("没有对应的家长信息！",2);
	 				}
	 			}
	 		});
	 		var editindex=layer.open({
				type: 1, 
			    title: '修改家长信息',
		        area: ['830px', '95%'],
		        shade: 0.8,
		        maxmin: true,
		        content: $('#editparFrom'),
		        cancel: function(){ 
				   $(".editparlayer-con").css("display","none");
				}
		    });
	    	layer.full(editindex);
	    	
	    	/*点击保存修改家长信息*/
	        form.on('submit(editParent)', function(pardata){
	        	var realname=$("#inpeditrealName").val();
	        	var sex=$("input[name='inpeditsex']:checked").val();
	        	var birthday=$("#inpeditbirthday").val();
	        	var address=$("#inpeditaddress").val();
	    		$.ajax({
	    			url: '../sysparentmanagement/updateparent',  
	    	        type: 'get', 
	    	        dataType: 'json',  
	    	        data:{parinfoid:parentid,realname:realname,
	    	        	sex:sex,birthday:birthday,address:address},
	    	        success: function (data) {
	    	          if (data.code == 10001) {
	    	        	  layer.confirm('家长信息修改成功！', {
	    					  btn: ['确定'],
	    					  icon:1
	    				  },function(){
	    					  table.reload("parinfoid", {
	    			              where: {
	    			            	  keyword:data.code=='10001'
	    			              }
	    			          });	
	    					  layer.closeAll();
	    					  $(".editparlayer-con").css("display","none");
	    				  });
	    	          } else {  
	    	        	  layer.alert('家长信息修改失败！', {icon: 2});
	    	          }  
	    	        }
	    		}); 
	    	});
	    }
	    /*删除家长信息*/
	    else if(obj.event === 'btndelparent'){
	    	var parentid=data.parentid;
	    	layer.confirm('确定要删除么？', {
			  btn: ['确定','取消'],
			  icon:3
			}, function(){
				$.ajax({
					url: '../sysparentmanagement/deleteparent',  
        	        type: 'get', 
        	        dataType: 'json',  
        	        data:{parentid:parentid},
	        		success:function(data){
	        			if(data.code == 10001){
	        				layer.alert(data.msg, {
							    btn: ['知道了'],
	        					icon:1
							}, function(){
								table.reload("parinfoid", {
					                where: {
					                	keyword:data.code=='10001'
					                }
					            });	
								layer.closeAll();
							});		            				 
	        			}
	        			else{
	        				layer.alert(data.msg,2);
	        			}
	        		},
	        		error:function(){
	        			layer.msg("发生未知错误！！！",2);
	        		},
	        	});   
			}, function(){ });
	    }
    }); 
});